import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

public class AddCompetatives extends HttpServlet
{
	public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException
	{
		doPost(request,response);
	}//doGet

	public void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException
	{
		RequestDispatcher dispatcher = null;
		String event = request.getParameter("eventAction");
		if( event.equals("exit") )
		{
			dispatcher = request.getRequestDispatcher("/jsp/OperatorActivities.jsp");
			dispatcher.forward(request,response);
		}
		try
		{
			int outid = Integer.parseInt(request.getParameter("outid")) ;
			String day = request.getParameter("day");
			String month = request.getParameter("month");
			String year = request.getParameter("year");
			String date = year + "-" + month + "-" + day;
			String d = day;
			String m = month;
		    int datee = Integer.parseInt(day);
			if(datee<=9){ d = "0"+d;}
			int month1 = Integer.parseInt(month);
			if(month1<=9){ m = "0"+m;}
			String dateee = d + "-" + m + "-" + year;
			System.out.println("Date::"+date);
			int airtel = Integer.parseInt(request.getParameter("airtel"));
			int hutch = Integer.parseInt(request.getParameter("hutch"));
			int idea = Integer.parseInt(request.getParameter("idea"));
			int reliance = Integer.parseInt(request.getParameter("reliance"));
			int indicom = Integer.parseInt(request.getParameter("indicom"));
			int bsnl = Integer.parseInt(request.getParameter("bsnl"));

			Class.forName("com.mysql.jdbc.Driver").newInstance();
			Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/airtel","root","root");
			Statement stmt = con.createStatement();
			int id = 0;
			ResultSet rs = stmt.executeQuery("select max(id) from competative");
			if(rs.next())
			{
				if( rs.getString(1) != null )
				{
					id = Integer.parseInt(rs.getString(1));
				}
			}
			id++;
			
			int x = stmt.executeUpdate("insert into competative values('"+id+"','"+date+"','"+airtel+"','"+idea+"','"+hutch+"','"+bsnl+"','"+reliance+"','"+indicom+"','"+outid+"')");
			
			Collection vector = new Vector();
			rs = stmt.executeQuery("select id,name from outletdetails");
			while(rs.next())
			{
				vector.add(rs.getString(1));
				vector.add(rs.getString(2));
			}   
			request.setAttribute("outnames",vector);
			if(x == 1)
			{
				request.setAttribute("msg", new String("Competative Data Inserted Succesfully on the Date:"+dateee+"."));
				dispatcher = request.getRequestDispatcher("/jsp/Competatives.jsp");
				dispatcher.forward(request,response);
			}
		}//try
		catch(Exception e)
		{
            e.printStackTrace();
		}//catch
	}//doPost
}//class